<template>
  <view>
    <view class="goods-item">
      <!-- 左侧图片 -->
      <view class="goods-item-left">
        <!-- 商品的选中状态与商品状态值关联 -->
        <radio :checked="goods.goods_state" color="#f00000" v-if="showRadio" @click="radioHandler"></radio>
        <image :src="goods.goods_small_logo||defaultPic" class="goods-img"></image>
      </view>
      <!-- 右侧名称和价格展示 -->
      <view class="goods-item-right">
        <view class="goods-name">{{goods.goods_name}}</view>
        <view class="goods-info">
          <!-- 价格展示区 -->
          <view class="goods-price">
            ￥{{goods.goods_price | toFixed}}
          </view>  
          <!-- 商品数量展示区 -->
          <uni-number-box :min="1" :value="goods.goods_count" v-if="showNum" @change="numChangeHandler"></uni-number-box>
        </view> 
      </view>    
    </view>
  </view>
</template>

<script>
  export default {
    props: {
      goods: {
        type: Object,
        default: {}
      },
      showNum:{
        type: Boolean,
        default:false
      },
      showRadio:{
        type: Boolean,
        // 默认不展示单选按钮
        default: false
      }
    },
    methods:{
      radioHandler(e){
        console.log(e)
        // 子组件触发父组件的事件   
        this.$emit("radioChange",({
          goods_id: this.goods.goods_id,
          goods_state: this.goods.goods_state
        }))
      },
      // 改变数字盒子时触发事件
      numChangeHandler(val){
        console.log(val) 
      
        this.$emit("numChange",{
          goods_id: this.goods.goods_id,
          goods_count:val-0,
        })
      }
    },
    // 定义过滤器对象
    filters:{
      toFixed(num){
        return Number(num).toFixed(2)
      }
    },
    data() {
      return {
defaultPic: 'https://img3.doubanio.com/f/movie/8dd0c794499fe925ae2ae89ee30cd225750457b4/pics/movie/celebrity-default-medium.png',
      };
    }
  }
</script>

<style lang="scss">

    .goods-item {
      display: flex;
      border-bottom: 1px solid #f0f0f0;

      .goods-item-left {
        display: flex;
        justify-content: space-between;
        align-items: center;
        radio{
          margin-left: 2px;
        }
        .goods-img {
          width: 100px;
          height: 100px;
          padding: 3px;
        }
      }

      .goods-item-right {
        padding: 3px;
        display: flex;
        flex-direction: column;
        justify-content: space-between;

        .goods-name {
          font-size: 13px;
        }

        .goods-info {
          display: flex;
          justify-content: space-between;
          align-items: center;
          .goods-price {
            color: #f00000;
            font-size: 16px;
          }
          .uni-number-box{
            
          }
        }
      }
    }

</style>
